package com.zhsm.fuxi.mvp.ui.dialog

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.kyleduo.switchbutton.SwitchButton
import com.zhsm.fuxi.R
import com.zhsm.fuxi.base.BaseDialog
import com.zhsm.fuxi.mvp.ui.adapter.ReportInRecyclerAdapter
import kotlinx.android.synthetic.main.dialog_reports_intrrior.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by GuZhC on 2018/11/13.
 * describe:内饰报备弹窗
 */
class ReportsInteriorDialog(context: Context) : BaseDialog(context) {

    val data: MutableList<Any?>? = ArrayList()
    val adapter = ReportInRecyclerAdapter(data)
    override fun getLayoutId(): Int {
        return R.layout.dialog_reports_intrrior
    }

    override fun getGravity(): Int {
        return BaseDialog.GRAVITY_CENTER
    }

    override fun isCanceledOnTouchOutside(): Boolean {
        return true
    }

    override fun initView() {
        super.initView()
        var layoutManager = LinearLayoutManager(context)
        data!!.add("")
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        reportsInteriorRecycle.layoutManager = layoutManager
        reportsInteriorRecycle.adapter = adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            if (position + 1 == data?.size) {
                monClickTakePhoto?.takePic()
            }
        }
        setClick()
        dialogRoot.onClick { dismiss() }
    }

    val keys = ArrayList<String>()
    fun addPic(url: String, key: String) {
        adapter.addData(data!!.size - 1, url)
        keys.add(key)
    }

    fun setClick() {
        reportsInteriorSw.onClick {
            if (reportsInteriorSw.isChecked) reportsInteriorLl.visibility = View.VISIBLE
            else reportsInteriorLl.visibility = View.GONE
        }

        reportsInteriorOut.onClick { dismiss() }
        reportsInteriorOk.onClick {
            if (findViewById<SwitchButton>(R.id.reportsInteriorSw).isChecked)
                monClickTakePhoto?.reportsInterior(true, reportsInteriorContent.text.toString().trim(), keys)
            else
                monClickTakePhoto?.reportsInterior(false, null, null)
        }
    }

    interface onClickTakePhoto {
        //拍照
        fun takePic()

        fun reportsInterior(isDamage: Boolean, dic: String?, pics: ArrayList<String>?)
    }

    //
    private var monClickTakePhoto: onClickTakePhoto? = null

    fun setOnClickTakePhoto(mOnClickTakePhoto: onClickTakePhoto?) {
        this.monClickTakePhoto = mOnClickTakePhoto
    }

    fun startCuntDown(s: String) {
        reportsInteriorTime.text = s
    }
}